# Maintainer: sxe <sxxe@gmx.de>
# Based on the wine package in the community repository



DEBUG=0
pkgname=wine-git
_gitname="wine"
pkgver=1.9.19.r168.g2035a8c
pkgrel=1
pkgdesc="A compatibility layer for running Windows programs. Latest GIT version."
url="http://www.winehq.com"
arch=('i686' 'x86_64')
license=('LGPL')
install='wine-git.install'
patchfile1="WGL-ShareList-Hack.patch"
patchfile2="100_crt_debugger_hook.patch"
source=('wine-git::git://source.winehq.org/git/wine.git'
        '30-win32-aliases.conf'
        "${patchfile1}::https://raw.githubusercontent.com/Unity3D-Wine-Support/Unity3D-on-Wine/master/Compile-Wine/WGL-ShareList-Hack.patch"
        "${patchfile2}"
        )

md5sums=('SKIP'
         '1ff4e467f59409272088d92173a0f801'
         'fb9352fff3b87b8120b333e4a4c46f5b'
         '76c479485ed7568ac4d2710b998ed2af')
sha512sums=('SKIP'
            '6e54ece7ec7022b3c9d94ad64bdf1017338da16c618966e8baf398e6f18f80f7b0576edf1d1da47ed77b96d577e4cbb2bb0156b0b11c183a0accf22654b0a2bb'
            '64217eb8a0cf4e1a0a371480fef58c1402475aed7f3e256cbf75858906d218b8a7c5ceb3a8baab1c81eb8d8fe9c4a762251ba928dd44a580cfbe10ad8bb8984c'
            '9bf70bfa9d743f5af3e39e36f7975945221bf87bff78c9b10236b534dfb8d18847ce667341eda1e63a0342ea4d19b2c42e9177cac286b3877493759719202720')


#CFLAGS="-march=native -O2 -pipe -fstack-protector-strong --param=ssp-buffer-size=4 -Wno-trigraphs -fno-omit-frame-pointer -ftrack-macro-expansion=2"
CFLAGS="-march=native -Ofast -fno-fast-math -pipe -D_FORTIFY_SOURCE=2"
CPPFLAGS="${CFLAGS}"
CXXFLAGS="${CFLAGS}"

LDFLAGS="-Wl,-O1,--sort-common,--as-needed,-z,relro"

if test -n "$DEBUG" -a "$DEBUG" -eq "1" ; then
  dbg="-g3 -DDEBUG -O1 -fvar-tracking-assignments"
  export CFLAGS+=" $dbg"
  export CXXFLAGS+=" $dbg"
  export CPPFLAGS+=" $dbg"
  msg2 "CFLAGS=$CFLAGS"
  msg2 "CXXFLAGS=$CXXFLAGS"
  msg2 "CPPFLAGS=$CPPFLAGS"
  #options-=('strip')
  options+=('!strip')
fi

_depends=(
  fontconfig      lib32-fontconfig
  libxcursor      lib32-libxcursor
  libxrandr       lib32-libxrandr
  libxdamage      lib32-libxdamage
  libxi           lib32-libxi
  gettext         lib32-gettext
  freetype2       lib32-freetype2
  glu             lib32-glu
  libsm           lib32-libsm
  gcc-libs        lib32-gcc-libs
  desktop-file-utils

  #unsure if needed:
  lib32-alsa-plugins lib32-libpulse
)

makedepends=(autoconf ncurses bison perl fontforge flex prelink
  'gcc>=4.5.0-2'  'gcc-multilib>=4.5.0-2'
  giflib          lib32-giflib
  libpng          lib32-libpng
  gnutls          lib32-gnutls
  libxinerama     lib32-libxinerama
  libxcomposite   lib32-libxcomposite
  libxmu          lib32-libxmu
  libxxf86vm      lib32-libxxf86vm
  libxml2         lib32-libxml2
  libldap         lib32-libldap
  lcms            lib32-lcms
  mpg123          lib32-mpg123
  openal          lib32-openal
  v4l-utils       lib32-v4l-utils
  alsa-lib        lib32-alsa-lib
  libxcomposite   lib32-libxcomposite
  mesa            lib32-mesa
  git
  libunwind #-git
)
  
optdepends=(
  giflib          lib32-giflib
  libpng          lib32-libpng
  libldap         lib32-libldap
  gnutls          lib32-gnutls
  lcms            lib32-lcms
  libxml2         lib32-libxml2
  mpg123          lib32-mpg123
  openal          lib32-openal
  v4l-utils       lib32-v4l-utils
  libpulse        lib32-libpulse
  alsa-plugins    lib32-alsa-plugins
  alsa-lib        lib32-alsa-lib
  libjpeg-turbo   lib32-libjpeg-turbo
  libxcomposite   lib32-libxcomposite
  libxinerama     lib32-libxinerama
  ncurses         lib32-ncurses
  libcl           lib32-libcl
  cups
  samba           dosbox
)

# Check if libowfat is installed.
# It has to be removed because WINE cannot be build if installed.
# Thanks to haagch 
if [ -f /usr/lib/libowfat.a ]; then
    msg2 "Error: libowfat.a detected. Please remove the libowfat package. WINE cannot be build if installed."
    exit 0;
fi

if [[ $CARCH == i686 ]]; then
  # Strip lib32 etc. on i686
  _depends=(${_depends[@]/*32-*/})
  makedepends=(${makedepends[@]/*32-*/} ${_depends[@]})
  makedepends=(${makedepends[@]/*-multilib*/})
  optdepends=(${optdepends[@]/*32-*/})
  conflicts=('wine')
else
  makedepends=(${makedepends[@]} ${_depends[@]})
  provides=('bin32-wine' 'wine-wow64' 'wine' )
  conflicts=('bin32-wine' 'wine-wow64' 'wine')
  replaces=('bin32-wine')

fi

pkgver() {
  #date +%Y%m%d
  cd "$srcdir/$pkgname"
  git describe --always --long | sed -E 's/([^-]*-g)/r\1/;s/-/./g;s/^wine.//'
}

prepare() {
  cd "$srcdir/$pkgname"

  #FIXME patch -Np1 -i "$srcdir/$patchfile1"
  #FIXME patch -Np1 -i "$srcdir/$patchfile2"

}

build() {
  cd "$srcdir"


  # ncurses fix
  sed -i 's|libncurses|libncursesw|g' "$srcdir/$pkgname/configure"
  sed -i 's|lncurses|lncursesw|g' "$srcdir/$pkgname/configure"

  # Get rid of old build dirs
  rm -rf $pkgname-{32,64}-build
  mkdir $pkgname-32-build

  # These additional CPPFLAGS solve FS#27662 and FS#34195
  export CPPFLAGS="${CPPFLAGS/-D_FORTIFY_SOURCE=2/} -D_FORTIFY_SOURCE=0 "
  export CXXFLAGS="$CPPFLAGS"

  if [[ $CARCH == x86_64 ]]; then

    msg2 "Building Wine-64..."

    mkdir $pkgname-64-build
    cd "$srcdir/$pkgname-64-build"
    ../$pkgname/configure \
      --prefix=/usr \
      --libdir=/usr/lib \
      --with-x \
      --without-gstreamer \
      --enable-win64
    # Gstreamer was disabled for FS#33655

    make -j4 CFLAGS+=" -mstackrealign -mincoming-stack-boundary=4" CXXFLAGS+=" -mstackrealign -mincoming-stack-boundary=4"

    _wine32opts=(
      --libdir=/usr/lib32
      --with-wine64="$srcdir/$pkgname-64-build"
    )

    export PKG_CONFIG_PATH="/usr/lib32/pkgconfig"
  fi

  msg2 "Building Wine-32..."
  cd "$srcdir/$pkgname-32-build"
  ../$pkgname/configure \
    --prefix=/usr \
    --with-x \
    --without-gstreamer \
    "${_wine32opts[@]}"

  # These additional flags solve FS#23277
  make -j4 CFLAGS+=" -mstackrealign -mincoming-stack-boundary=2" CXXFLAGS+=" -mstackrealign -mincoming-stack-boundary=2"
}

package() {
#  provides[2]="wine=$(cd "$srcdir/$pkgname"  git describe --always --long | cut -d '-' -f 2)"
  depends=(${_depends[@]})

  gitver=$(cd "$srcdir/$pkgname" && git describe --long | sed -r 's/^wine-//;s/([^-]*-g)/r\1/;s/-/./g')
  provides=("wine=$gitver") 
  if [[ $CARCH == x86_64 ]]; then   
    provides[2]="bin32-wine=$gitver"
    provides[3]="wine-wow64=$gitver"
  fi
  echo "111111111111111111 ${provides[@]} 1111111111111111111111"

  msg2 "Packaging Wine-32..."
  cd "$srcdir/$pkgname-32-build"

  if [[ $CARCH == i686 ]]; then
    make prefix="$pkgdir/usr" install
  else
    make prefix="$pkgdir/usr" \
      libdir="$pkgdir/usr/lib32" \
      dlldir="$pkgdir/usr/lib32/wine" install

    msg2 "Packaging Wine-64..."
    cd "$srcdir/$pkgname-64-build"
    make prefix="$pkgdir/usr" \
      libdir="$pkgdir/usr/lib" \
      dlldir="$pkgdir/usr/lib/wine" install
  fi

  # Font aliasing settings for Win32 applications
  install -d "$pkgdir"/etc/fonts/conf.{avail,d}
  install -m644 "$srcdir/30-win32-aliases.conf" "$pkgdir/etc/fonts/conf.avail"
  ln -s ../conf.avail/30-win32-aliases.conf "$pkgdir/etc/fonts/conf.d/30-win32-aliases.conf"
}
